from enum import Enum
from logging import getLogger

from fitz import Document

logger = getLogger(__name__)


class ReceiptType(Enum):
    GENERAL_RECEIPT = 0
    VAT_RECEIPT = 1
    UNKNOWN = 2


def get_receipt_type_name(receipt_type: ReceiptType) -> str:
    if receipt_type == ReceiptType.GENERAL_RECEIPT:
        return "电子发票（普通发票）"
    elif receipt_type == ReceiptType.VAT_RECEIPT:
        return "增值税电子普通发票"
    else:
        return "未知发票类型"


def get_receipt_type(doc: Document) -> ReceiptType:
    if doc.page_count != 1:
        raise Exception("发票不是单页")

    page = doc.load_page(0)
    text_contents = page.get_text("text")

    if text_contents.find("电子发票（普通发票）") != -1:
        return ReceiptType.GENERAL_RECEIPT
    elif text_contents.find("增值税电子普通发票") != -1:
        return ReceiptType.VAT_RECEIPT
    else:
        # 如果没有找到匹配的发票类型，则返回 UNKNOWN
        return ReceiptType.UNKNOWN
